<?php
declare (strict_types=1);

namespace app\admin\logic\site\discovery;

use app\admin\logic\system\Log;
use app\admin\model\site\discovery\Discovery as DiscoveryModel;
use app\admin\model\site\discovery\DiscoveryData;
use app\admin\model\site\PersonalData;
use app\common\exception\LogicException;

class Discovery extends DiscoveryModel
{

    /**
     * 获取列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/20
     *
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList()
    {
        $items = $this->with(['discoveryData', 'iconUrl'])->order(['sort' => 'asc', 'update_time' => 'desc'])->paginate($this->getListRows());

        foreach ($items as &$item) {
            $discoveryData = $item->discoveryData;
            if ($discoveryData) {
                $item['icon_url'] = $discoveryData->icon ? $discoveryData->icon_url : '';
                $item['title']    = $discoveryData->title ?: $item['title'];
                $item['status']   = $discoveryData->status ?: 0;
                $item['sort']     = $discoveryData->sort ?: $item['sort'];
            }
        }
        unset($item);

        return $items;
    }

    /**
     * editItem
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/20
     *
     * @param $id
     * @param $param
     * @return Discovery|array|\think\Model
     */
    public function editItem($id, $param)
    {
        // 验证数据
        // $this->getValidate()->scene('edit')->failException()->check($check);
        //
        $item = $this->findOrFail($id);

        // 更改数据表
        $discoveryData = DiscoveryData::where('config_id', $id)->where('mhm_id', request()->mhm_id)->findOrEmpty();

        if ($discoveryData->isEmpty()) {
            $discoveryData->icon      = $item['icon'];
            $discoveryData->config_id = $item['id'];
            $discoveryData->title     = $item['title'];
            $discoveryData->status    = 1;
            $discoveryData->sort      = 1;
        }
        if ($param['title']) {
            $discoveryData->title = $param['title'];
        }
        if (isset($param['status'])) {
            $discoveryData->status = intval($param['status']);
        }
        if ($param['sort']) {
            $discoveryData->sort = $param['sort'];
        }

        if ($param['icon']) {
            $discoveryData->icon = $param['icon'];
        }

        // $item->title = $param['title'];
        // $item->icon  = $param['icon'];
        // // $item->group_id = $param['group_id'];
        // $item->status = $param['status'] ?? 1;

        if (!$discoveryData->save()) {
            throw new LogicException("编辑失败");
        }

        // 更新成功
        Log::write('更新手机端发现页【' . $item->getAttr('title') . '】', 'site_discovery', 'update', true);

        return $item;
    }

    /**
     * 编辑某个字段
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/20
     *
     * @param $id
     * @param $field
     * @param $param
     * @return Discovery|array|\think\Model
     */
    public function editItemField($id, $field, $param)
    {
        $item = $this->findOrFail($id);

        // 更改数据表
        $discoveryData = DiscoveryData::where('config_id', $id)->where('mhm_id', request()->mhm_id)->findOrEmpty();
        if ($discoveryData->isEmpty()) {
            $discoveryData->icon      = $item['icon'];
            $discoveryData->config_id = $item['id'];
            $discoveryData->title     = $item['title'];
            $discoveryData->status    = 1;
            $discoveryData->sort      = 1;
        }
        $discoveryData->$field = $param[$field];
        if (!$discoveryData->save()) {
            throw new LogicException("修改失败");
        }

        // 更新成功
        Log::write('更新手机端发现页【' . $item->getAttr('title') . '】', 'site_discovery', 'update', true);

        return $item;
    }
}
